package com.firesrc.dao.impl;

import java.util.List;

import jodd.db.DbThreadSession;
import jodd.db.oom.DbOomQuery;
import jodd.db.oom.sqlgen.DbEntitySql;

import com.firesrc.bean.MailDayAmount;
import com.firesrc.dao.MailDayAmountDao;
import com.firesrc.dao.SuperDao;
import com.firesrc.util.SystemUtil;

public class MailDayAmountDaoImpl extends SuperDao<MailDayAmount> implements MailDayAmountDao {

	@Override
	public boolean isTodayExist(String userName) {
		DbThreadSession dbThreadSession = new DbThreadSession(cp);
		DbOomQuery query = new DbOomQuery("select count(id) from Mail_Day_Amount where userName='" + userName
				+ "' and sendDate='" + SystemUtil.getCurrentDateStringYYYYMMDD() + "'");
		long executeCountAndClose = query.executeCountAndClose();
		dbThreadSession.closeSession();
		return executeCountAndClose > 0;
	}

	@Override
	public void updateAmount(MailDayAmount mailDayAmount) {
		DbThreadSession dbThreadSession = new DbThreadSession(cp);
		DbOomQuery query = new DbOomQuery("update Mail_Day_Amount set amount=amount+" + mailDayAmount.getAmount()
				+ " where userName='" + mailDayAmount.getUserName() + "' and sendDate='"
				+ SystemUtil.getCurrentDateStringYYYYMMDD() + "'");
		System.out.println("MailDayAmountDaoImpl.updateAmount()" + query);
		query.executeUpdateAndClose();
		dbThreadSession.closeSession();

	}

	@Override
	public List<MailDayAmount> getTodayList() {
		DbThreadSession dbThreadSession = new DbThreadSession(cp);
		List<MailDayAmount> list = DbEntitySql
				.findByColumn(MailDayAmount.class, "sendDate", SystemUtil.getCurrentDateStringYYYYMMDD()).query()
				.list();
		dbThreadSession.closeSession();
		return list;
	}

}
